package com.sg.service.biz.app.nb;

import cn.hutool.core.bean.BeanUtil;
import com.sg.dto.biz.app.req.*;
import com.sg.dto.biz.app.res.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbEvaSystem {
    /**
     * code:findLevelCode
     * name:M-查询找祖先列表（特殊方法）
     * desc:undefined
     * gen by moon at 8/29/2022, 12:43:24 PM
     **/
    @Trace(operationName = "M-查询找祖先列表（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryLookAncestorNodeListRespDto queryLookAncestorNodeList(QueryLookAncestorNodeListReqDto reqDto) {
        QueryLookAncestorNodeListRespDto retData = new QueryLookAncestorNodeListRespDto();
        String oriCode = null;
        if (reqDto.getEvaObjTargetCycleCode() != null && !reqDto.getEvaObjTargetCycleCode().isEmpty()) {
            oriCode = reqDto.getEvaObjTargetCycleCode();
        }

        if (reqDto.getAssistNodeTypeObjectCode() != null && !reqDto.getAssistNodeTypeObjectCode().isEmpty()) {
            oriCode = reqDto.getAssistNodeTypeObjectCode();
        }

        if (reqDto.getComCode() != null && !reqDto.getComCode().isEmpty()) {
            oriCode = reqDto.getComCode();
        }

        if (oriCode == null || oriCode.isEmpty()) {
            return retData;
        }
        int dealSize = 4;
        while (oriCode.length() >= dealSize) {
            String oneCode = oriCode.substring(0, dealSize);
            if (oneCode != null && !oneCode.isEmpty()) {
                EvaObjTargetCycleDto elm = new EvaObjTargetCycleDto();
                elm.setEvaObjTargetCycleCode(oneCode);
                retData.getEvaObjTargetCycleList().add(elm);
                retData.getLookAncestorNodeList().add(oneCode);
            }
            dealSize = dealSize + 4;
        }
        retData.getLookAncestorNodeList().remove(oriCode);
        return retData;
    }

    /**
     * code:receptionService
     * name:M3-接收输入字段
     * desc:undefined
     * gen by moon at 8/29/2022, 12:46:34 PM
     **/
    @Trace(operationName = "M3-接收输入字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainReceiveFieldRespDto obtainReceiveField(ObtainReceiveFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ObtainReceiveFieldRespDto.class);
    }

    /**
     * code:getDataCount
     * name:M3-数据集计算完成数量（特殊方法）
     * desc:undefined
     * gen by moon at 9/13/2022, 2:56:53 AM
     **/
    @Trace(operationName = "M3-数据集计算完成数量（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryDataSetCountQuantityAccomplishDetailRespDto queryDataSetCountQuantityAccomplishDetail(QueryDataSetCountQuantityAccomplishDetailReqDto reqDto) {
        QueryDataSetCountQuantityAccomplishDetailRespDto retData = new QueryDataSetCountQuantityAccomplishDetailRespDto();
        Long count = 0L;
        for (BizAppCheckItemStatDto one : reqDto.getBizAppCheckItemStatList()) {
            if (one.getIsComplete().equals("TRUE")) {
                count++;
            }
        }
        retData.setCalcCount(count);
        return retData;
    }

    /**
     * code:getDataCount
     * name:M3数据集计算父子状态完成数量（特殊方法）
     * desc:undefined
     * gen by moon at 9/13/2022, 2:59:55 AM
     **/
    @Trace(operationName = "M3数据集计算父子状态完成数量（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public CalculateDataSetCountFatherStateAchieveNumRespDto calculateDataSetCountFatherStateAchieveNum(CalculateDataSetCountFatherStateAchieveNumReqDto reqDto) {
        // TODO ruizhe skai dong ; 9/13/2022, 2:59:55 AM
        return new CalculateDataSetCountFatherStateAchieveNumRespDto();
    }

    /**
     * code:collectionRemoveDuplicateData
     * name:M去重我的下属（特殊方法）
     * desc:undefined
     * gen by moon at 9/13/2022, 4:00:24 PM
     **/
    @Trace(operationName = "M去重我的下属（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public UnrepeatedMySubordinateRespDto unrepeatedMySubordinate(UnrepeatedMySubordinateReqDto reqDto) {
        UnrepeatedMySubordinateRespDto retData = new UnrepeatedMySubordinateRespDto();
        Map<String, VirtualOrgMemberDto> indexInfo = new HashMap<>();
        for (VirtualOrgMemberDto oneMember : reqDto.getVirtualOrgMemberList()) {
            indexInfo.put(oneMember.getVirtualOrgInductionRecordId(), oneMember);
        }
        retData.setVirtualOrgMemberList(indexInfo.values().stream().map(p -> BeanUtil.toBean(p, VirtualOrgMemberDto.class)).collect(Collectors.toList()));
        return retData;
    }

    /**
     * code:receptionService
     * name:M3-牵头目标安排房间
     * desc:undefined
     * gen by moon at 9/22/2022, 12:23:16 AM
     **/
    @Trace(operationName = "M3-牵头目标安排房间")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementBizConstructAccessParameterFieldRespDto implementBizConstructAccessParameterField(ImplementBizConstructAccessParameterFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementBizConstructAccessParameterFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M3获取接收外部字段(特殊方法）
     * desc:undefined
     * gen by moon at 9/22/2022, 2:40:48 AM
     **/
    @Trace(operationName = "M3获取接收外部字段(特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainReceiveOutFieldRespDto obtainReceiveOutField(ObtainReceiveOutFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ObtainReceiveOutFieldRespDto.class);
    }

    /**
     * code:collectionsIntersectionsData
     * name:M3执行取与变更字段列表交集结果（特殊方法）
     * desc:undefined
     * gen by moon at 11/20/2022, 12:32:32 AM
     **/
    @Trace(operationName = "M3执行取与变更字段列表交集结果（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementTakeChangeColumnOverlapRespDto implementTakeChangeColumnOverlap(ImplementTakeChangeColumnOverlapReqDto reqDto) {
        ImplementTakeChangeColumnOverlapRespDto retData = new ImplementTakeChangeColumnOverlapRespDto();
        List<ChangeColumnDto> filteredList = new ArrayList<>();
        Map<String, EvaObjTargetCycleWeightDto> retIndex = new HashMap<>();

        for (ChangeColumnDto one : reqDto.getChangeColumnList()) {
            if (reqDto.getBizAppChangeColList().contains(one.getEntityId())) {
                filteredList.add(one);
                EvaObjTargetCycleWeightDto elm = new EvaObjTargetCycleWeightDto();
                elm.setObjTargetCycleWeightSetId(one.getEntityId());
                retData.getEvaObjTargetCycleWeightList().add(elm);
                retIndex.put(one.getEntityId(), elm);
            }
        }


        for (ChangeColumnDto change : filteredList) {
            EvaObjTargetCycleWeightDto elm = retIndex.get(change.getEntityId());
            Field[] declaredFields = elm.getClass().getDeclaredFields();
            Map<String, Field> fieldIndex = new HashMap<>();
            for (Field field : declaredFields) {
                fieldIndex.put(field.getName(), field);
            }
            if (ObjectUtils.isNotEmpty(elm)) {
                Field fileObj = fieldIndex.get(change.getObjectPropertyCode());
                if (fileObj != null) {
                    fileObj.setAccessible(true);
                    try {
                        if (fileObj.getType().equals(Double.class)) {
                            fileObj.set(elm, Double.valueOf(change.getMetaDataColumnContent()));
                        } else if (fileObj.getType().equals(Long.class)) {
                            fileObj.set(elm, Long.valueOf(change.getMetaDataColumnContent()));
                        } else {
                            fileObj.set(elm, change.getMetaDataColumnContent());
                        }

                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }


        }


        return retData;
    }

    /**
     * code:receptionService
     * name:M3约定默认值“1”
     * desc:undefined
     * gen by moon at 2/10/2023, 12:38:21 AM
     **/
    @Trace(operationName = "M3约定默认值“1”")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAccessParameterBizFieldRespDto implementAccessParameterBizField(ImplementAccessParameterBizFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementAccessParameterBizFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M3接收上文出参字段值
     * desc:undefined
     * gen by moon at 4/18/2023, 11:22:19 PM
     **/
    @Trace(operationName = "M3接收上文出参字段值")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAcceptFieldRespDto implementAcceptField(ImplementAcceptFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementAcceptFieldRespDto.class);

    }

    /**
     * code:receptionService
     * name:M3约定指标层级：1
     * desc:undefined
     * gen by moon at 6/2/2023, 6:32:03 PM
     **/
    @Trace(operationName = "M3约定指标层级：1")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementGainInParameterFieldsRespDto implementGainInParameterFields(ImplementGainInParameterFieldsReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementGainInParameterFieldsRespDto.class);

    }

    /**
     * code:receptionService
     * name:初始化：0（不参与审核）
     * desc:undefined
     * gen by moon at 12/7/2023, 6:24:18 PM
     **/
    @Trace(operationName = "初始化：0（不参与审核）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAcceptFieldByStandardDataRespDto implementAcceptFieldByStandardData(ImplementAcceptFieldByStandardDataReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementAcceptFieldByStandardDataRespDto.class);
    }
    //手工接入方法
}
